package com.yzk.auction.utils;

import java.util.List;

/**
 * Author: 园中葵小组成员
 * Description:分页模型对象
 */
public class PageModel {
	private int page;					//页号
	private int totalPages;				//总页数
	private int rows;					//每页记录数
	private int totalRows;				//总记录数
	private int pageStartRow;			///当前页从第n行开始
	private int pageEndRow;				//是否存在下一页
	private boolean hasNextPage;		//是否存在下一页
	private boolean hasPreviousPage;	//是否存在上一页
	private List pageData;				//当前页面数据

	public PageModel() {

	}
	/**
	 * 初始化PageModel对象，计算分页属性
	 * @param data 原始数据集合
	 * @param page 页号
	 * @param rows 每页记录数
	 */
	public PageModel(List data,int page,int rows) {
		this.page = page;
		this.rows = rows;
		totalRows = data.size();
		//总页数计算规则：总行数/每页记录数，能整除页数取整，不能整除向上取整
		//Math.ceil 浮点数向上取整 Math.floor 浮点数向下取整，结果是double,需要创建一个double对象，将结果传入,通过intValue()获取整数部分
		//注意事项：Java中两个整数相除结果是整数，需转为浮点数运算
		totalPages = new Double(Math.ceil(totalRows / (rows * 1f))).intValue();
		pageStartRow = (page - 1) * rows;
		pageEndRow = page * rows;
		//得到分页数据，subList包含左边，不包含右边
		//最后一页，可能出现下标越界
		if(pageEndRow > totalRows) {
			pageEndRow = totalRows;
		}
		pageData = data.subList(pageStartRow, pageEndRow);
		//是否存在上下页判断
		if(page > 1) {
			hasPreviousPage = true;
		} else {
			hasPreviousPage = false;
		}
		if(page < totalPages) {
			hasNextPage = true;
		} else {
			hasNextPage = false;
		}
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getTotalPages() {
		return totalPages;
	}
	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}
	public int getRows() {
		return rows;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	public int getTotalRows() {
		return totalRows;
	}
	public void setTotalRows(int totalRows) {
		this.totalRows = totalRows;
	}
	public int getPageStartRow() {
		return pageStartRow;
	}
	public void setPageStartRow(int pageStartRow) {
		this.pageStartRow = pageStartRow;
	}
	public int getPageEndRow() {
		return pageEndRow;
	}
	public void setPageEndRow(int pageEndRow) {
		this.pageEndRow = pageEndRow;
	}
	public boolean isHasNextPage() {
		return hasNextPage;
	}
	public void setHasNextPage(boolean hasNextPage) {
		this.hasNextPage = hasNextPage;
	}
	public boolean isHasPreviousPage() {
		return hasPreviousPage;
	}
	public void setHasPreviousPage(boolean hasPreviousPage) {
		this.hasPreviousPage = hasPreviousPage;
	}
	public List getPageData() {
		return pageData;
	}
	public void setPageData(List pageData) {
		this.pageData = pageData;
	}

	@Override
	public String toString() {
		return "PageModel{" +
				"page=" + page +
				", totalPages=" + totalPages +
				", rows=" + rows +
				", totalRows=" + totalRows +
				", pageStartRow=" + pageStartRow +
				", pageEndRow=" + pageEndRow +
				", hasNextPage=" + hasNextPage +
				", hasPreviousPage=" + hasPreviousPage +
				", pageData=" + pageData +
				'}';
	}
}